package pers.cz.controller;

import cn.hutool.core.date.DateUtil;
import pers.cz.model.HostsAddress;
import pers.cz.pub.RespBean;
import pers.cz.service.TestConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @program: PostGirl-panent
 * @description: TestConfig
 * @author: Cheng Zhi
 * @create: 2021-08-20 15:29
 **/
@RestController
@Api(tags = "测试配置")
@RequestMapping("/api/TestConfig")
public class TestConfigController {

    @Autowired
    TestConfigService service;

    @ApiOperation(value = "获取所有的测试地址")
    @GetMapping(path = "/getAllHostsAddress")
    public List<HostsAddress> getAllHostsAddress() {
        return service.getAllHostsAddress();
    }

    @ApiOperation(value = "分页获取所有测试地址")
    @PostMapping(path="/getAllHostsAddressByPage")
    public List<HostsAddress> getAllHostsAddressByPage(@RequestBody Map map) {

        int pageStart = (int) map.get("pageStart");
        int pageLimit = (int) map.get("pageLimit");

        return service.getAllHostsAddresByPage(pageStart, pageLimit);
    }

    @ApiOperation(value = "修改测试地址")
    @PostMapping(path="/updateHostsAddress")
    public RespBean updateHostsAddress(@RequestBody Map map) {

        Integer id = (Integer) map.get("id");
        String address = (String) map.get("hostAddress");
        String enviName = (String) map.get("enviName");
        String ext1 = (String) map.get("ext1");
        HostsAddress hostsAddress = new HostsAddress();
        hostsAddress.setId(Long.valueOf(id));
        hostsAddress.setEnviName(enviName);
        hostsAddress.setHostAddress(address);
        hostsAddress.setUpdatedTime(DateUtil.date());
        hostsAddress.setExt1(ext1);

        int count = service.updateHostsAddress(hostsAddress);
        return RespBean.ok("成功修改[" + count + "]条");
    }

    @ApiOperation(value = "删除测试地址")
    @PostMapping(path="/deleteHostsAddress")
    public RespBean deleteHostsAddress(@RequestBody Map map) {
        Integer id = (Integer) map.get("id");

        int count = service.delteHostsAddress(Long.valueOf(id));
        return RespBean.ok("成功删除[" + count + "]条");
    }

    @ApiOperation(value = "添加测试地址")
    @PostMapping(path="/addHostsAddress")
    public RespBean addHostsAddress(@RequestBody Map map) {

        String address = (String) map.get("hostAddress");
        String enviName = (String) map.get("enviName");
        String ext1 = (String) map.get("ext1");
        HostsAddress hostsAddress = new HostsAddress();
        hostsAddress.setEnviName(enviName);
        hostsAddress.setHostAddress(address);
        hostsAddress.setUpdatedTime(DateUtil.date());
        hostsAddress.setExt1(ext1);
        int count = service.addHostsAddress(hostsAddress);
        return RespBean.ok("成功添加[" + count + "]条");
    }

    @ApiOperation(value = "获取测试地址总数")
    @GetMapping(path="/getAddressTotalNum")
    public Integer getAddressTotalNum() {

        Integer count = service.getAddressTotalNum();
        return count;
    }

    @ApiOperation(value = "查询测试地址")
    @PostMapping(path="/queryAddressByFuzzy")
    public List<HostsAddress> queryAddressByFuzzy(@RequestBody Map map) {

        String enviName = (String) map.get("enviName");
        String hostAddress = (String) map.get("hostAddress");

        return service.queryAddress(enviName, hostAddress);
    }

}
